<?php
include "Model/DBUtil.php";
	
	class Member
	{
		private $id;
		private $username;
		private $password;
		private $firtName;
		private $lastName;
		
		public function __construct() { }
		
		public static function WithRow($row){
			$instance = new self();
			$instance->LoadRow($row);
			return $instance;
		}
		
		public static function WithParams($username, $password)
		{
			$instance = new self();
			$instance->SetUsername($username);
			$instance->SetPassword($password);
			return $instance;
		}
		
		public function GetUsername(){
			return $this->username;
		}
		
		public function SetUsername($username){
			$this->username = $username;
		}
		
		public function GetPassword(){
			return $this->password;
		}
		
		public function SetPassword($password){
			$this->password = $password;
		}
		
		public function GetId(){
			return $this->id;
		}
		
		/**
 		* returns an a Member object for the specified Username
 		* @param unknown_type $conn
 		* @param string $username
 		*/
		public static function GetMemberByUsername($conn, $username){
			$results = DBUtil::ExecuteProcedureQuery($conn, "BEGIN MEMBER_PKG.member_get_by_username('$username',:rc); END;");
			
			if (is_string($results)){
				return $results;
			}
			
			if (count($results) == 1){
				return Member::WithRow($results[0]);
			}
			//no match was found or more than one member was returned which should not happen
			return false;
		}
		
		public function Save($conn){
			$stmt = "BEGIN MEMBER_PKG.member_save('%s','%s','%s','%s'); END;";
			$stmt = sprintf($stmt, $this->username, $this->password, $this->firstName, $this->lastName);

			return DBUtil::ExecuteProcedureNonQuery($conn, $stmt);
		}
		
		private function LoadRow($row){
			$this->id = $row['ID'];
			$this->username = $row['USERNAME'];
			$this->password = $row['PASSWORD'];
		}
		
		public function __toString(){
        	return (string)$this->id;
    	}
		
	}
?>
